package com.orangelife.mobile.goodsOrder.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsOrder.biz.OrderBiz;
import com.orangelife.mobile.goodsOrder.biz.OrderHttp;
import com.orangelife.mobile.util.ToastHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 退回商品
 * Created by YangWei
 * on 2016/1/14.
 */
public class BackGoodActivity extends OrangeLifeBaseActivity {
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_title)
    RelativeLayout rlTitle;
    @Bind(R.id.tvReason)
    TextView tvReason;
    @Bind(R.id.etLogisticsCompany)
    EditText etLogisticsCompany;
    @Bind(R.id.etLogisticsID)
    EditText etLogisticsID;
    @Bind(R.id.tvInfo2)
    TextView tvInfo2;
    @Bind(R.id.btnSubmit)
    Button btnSubmit;

    private String applicationID;
    private Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_backgood);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        bundle = getIntent().getExtras();
        applicationID = bundle.getString("applicationID");
        tvTitle.setText("退回商品");
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backGood();
            }
        });
    }
    private Dialog dialog;
    private void getDataFromServer() {
        dialog = DialogHelper.getInstance().createLoadingDialog(this,
                Constant.LOADING);
        dialog.show();
    }
    private void backGood() {

        String logisticsCompany = etLogisticsCompany.getText().toString();
        String logisticsID = etLogisticsID.getText().toString();

        if (StringUtil.isBlank(logisticsCompany)) {
            ToastHelper.getInstance().displayToastWithQuickClose("请填写物流公司名称");
            return;
        }
        if (StringUtil.isBlank(logisticsID)) {
            ToastHelper.getInstance().displayToastWithQuickClose("请填写快递单号");
            return;
        }

        getDataFromServer();
        Map<String, Object> map = new HashMap<>();
        map.put("logisticsCompany", logisticsCompany);
        map.put("logisticsID", logisticsID);
        map.put("applicationID", applicationID);
        OrderHttp.postBackGood(map, handler);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String errInfo = JSONHelper.jsonToMap(msg.obj.toString())
                            .get("errInfo").toString();
                    if (errInfo.equals("OK")) {
                        DialogHelper.closeDialog(dialog);
                        ToastHelper.getInstance()._toast(getResources().getString(R.string.submit_success));
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        DialogHelper.closeDialog(dialog);
                        ToastHelper.getInstance()._toast(
                                getResources().getString(
                                        R.string.complaintes_sumbite_fail));
                    }
                    break;
                case 1011:
                    isLogin(dialog);
                    break;
                case Constant.WHAT_ERROR_HTTP:
                    ToastHelper.getInstance()._toast(String.valueOf(msg.obj));
                    break;
            }
            if (dialog != null) {
                dialog.dismiss();
            }
        }
    };
}
